<?php

class CItemShow{
	
	private $db;
	
	private $keyword;
	
	private $flid;
	
	private $fromTo;
	
	private $type;
	
	private $typeTable;
	
	private $typeFlTable;
	
	private $uid;
	
	private $where;
	
	private $order;
	
	private $ids;
			
	public function __construct(){
		
		$this->db=Zend_Registry::get('db');
		
	}
	
	public function setFlid($flid){
		
		$this->flid=$flid;
		
	}
	
	public function setFromTo($fromTo){
		
		$this->fromTo=$fromTo;
		
	}
	
	public function setType($type){
		
		$this->type=$type;
		
	}
	
	
	public function setTypeFlTable($typeFlTable){
		
		$this->typeFlTable=$typeFlTable;
		
	}
	
	public function setUid($uid){
		
		$this->uid=$uid;
		
	}
	
	public function setWhere($where){
		
		$this->where=$where;
		
	}
	
	public function setOrder($order){
		
		$this->order=$order;
		
	}
	
	public function setKeyWord($keyword){
		
		$this->keyword=$keyword;
		
	}
	
	public function setids($ids) {
		$this->ids = $ids;
	}
	
	public function getAllItemCounts($filter = ''){
		
		$db=$this->db;
		
		$itemTable=R_PRE.'items';
		
		$select = $db->select();

		$select->from($itemTable);
		
		if($this->where!=null){
			
			$typeTable=R_PRE.$this->type."s";
			
			$select->join($typeTable, $itemTable.".itemid = $typeTable.itemid");
			
			$select->where($this->where);
	
		}
		
		if ($this->type == 'product') {
            $select->joinLeft(R_PRE . 'product_attr', $itemTable.".itemid = " . R_PRE . "product_attr.itemid", array('attr_name', 'attr_value'));
            if ($filter) {  
            	foreach ($filter as $k=>$v) {     
            		$select->join(array('attr_' . $k=>R_PRE . 'product_attr'), $itemTable.".itemid = attr_".$k.".itemid", array());     	
            		$select->where('attr_' . $k .".attr_id_ex = " . $v . ' and attr_' . $k . '.attr_name = "' . $k . '"');  
            	}           	
            }
            $select->group($itemTable.'.itemid');
        }
		
		if($this->flid!=0&&$this->flid!=null){
		
			$select->where(' flid = ? ',$this->flid);
			
		}
		
		if($this->uid!=0&&$this->uid!=null){
			
			$select->where(' uid = ? ',$this->uid);
			
		}

	        $select->where('is_showed=1');

		if($this->keyword!=null){
			
			$typeTable=R_PRE.$this->type."s";
			
			$select->join($typeTable, "$itemTable.itemid = $typeTable.itemid", '*');
			
			$select->where("CONCAT($itemTable.subject,$typeTable.content) LIKE '%$this->keyword%'");
			
		}
		
		if($this->ids != '') {
			$typeTable=R_PRE.$this->type."s";
			
			$select->join($typeTable, "$itemTable.itemid = $typeTable.itemid", '*');
			
			$select->where($typeTable.'.productid in ('.$this->ids.')');
		}
		
		$select->where(' item_type = ? ',$this->type);
		
		if ($this->type == 'product' && $filter) {
			$str = $select->__toString();
			$myCache = Zend_Registry::get('myCache');
			$count = $myCache->load(md5($str));
			if (!$count) {
				$count = count($db->fetchAll($select));
				$myCache->save($count, md5($str));
			}
			return $count;
		}
		
		return count($db->fetchAll($select));
		
	}
	
	public function getAllItems($filter = ''){
		
		$db=$this->db;
		
		if($this->typeFlTable==null){
		
			$typeFlTable=R_PRE.$this->type."fls";
		
		}else{
			
			$typeFlTable=$this->typeFlTable;
			
		}
		
		$itemTable=R_PRE.'items';
		
		$typeTable=R_PRE.$this->type."s";
		
		$select = $db->select();
		
		$select->from($itemTable,'*');
		
		$select->join($typeTable, "$itemTable.itemid = $typeTable.itemid", '*');
		
		if ($this->type == 'product') {
            $select->joinLeft(R_PRE . 'product_attr', $itemTable.".itemid = " . R_PRE . "product_attr.itemid", array('attr_name', 'attr_value'));
			if ($filter) {
            	foreach ($filter as $k=>$v) {     
            		$select->join(array('attr_' . $k=>R_PRE . 'product_attr'), $itemTable.".itemid = attr_".$k.".itemid", array());     	
            		$select->where('attr_' . $k .".attr_id_ex = " . $v . ' and attr_' . $k . '.attr_name = "' . $k . '"');   
            	} 
            }
            $select->group( $itemTable.'.itemid');
        }
		
		if($this->flid!=null&&$this->flid!=0){
			
			$select->join($typeFlTable, "$itemTable.flid = $typeFlTable.flid", '*');
		
			$select->where($itemTable.'.flid = ? ',$this->flid);
			
		}else{
			
			$select->joinLeft($typeFlTable, "$itemTable.flid = $typeFlTable.flid", '*');
			
		}
		
		if($this->uid!=0&&$this->uid!=null){
			
			$select->where($itemTable.'.uid = ? ',$this->uid);
			
		}else{
			
			$select->join(R_PRE.'members', R_PRE.'members.uid = '.$itemTable.'.uid', '*');
			
		}
		
                $select->where('is_showed=1');

		if($this->where!=null){
			
			$select->where($this->where);
			
		}
		
		if($this->keyword!=null){
			
			$select->where("CONCAT($itemTable.subject,$typeTable.content) LIKE '%$this->keyword%'");
                       // $select->where("CONCAT($itemTable.subject,$typeTable.pattern_intro,$typeTable.content) LIKE '%$this->keyword%'");
			
		}
		
		if($this->ids != '') {
			$select->where($typeTable.'.productid in ('.$this->ids.')');
		}
		
		if($this->order!=null){
			
			$select->order($this->order);
			
		}
		
		if($this->fromTo!=null){
			
			$select->limit($this->fromTo['counts'], $this->fromTo['from']);
			
		}
		$results = $db->fetchAll($select);
		if ($this->type == 'product') {
	        foreach ($results as &$v) {
	        	$product = new CProduct();       	
	        	unset($v['attr_name']);
	        	unset($v['attr_value']);
	        	$attrs = $product->getAllAttr($v['itemid']);
	        	foreach ($attrs as $_v) {
	        		$v[$_v->attr_name] = $_v->attr_value;
	        	}
	        }
        }
		return $results;
		
	}
	
}
